using System;
using System.Linq;
using System.Web.Mvc;
using Icms.Core.Model;
using Icms.Core.Repository;
using Omu.Awesome.Mvc;

namespace Icms.WebUI.Controllers
{
    public class ReportTemplateIdLookupController : LookupController
    {
        private readonly IRepo<ReportTemplate> r;

        public ReportTemplateIdLookupController(IRepo<ReportTemplate> r)
        {
            this.r = r;
        }

        public override ActionResult SearchForm()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Search(string search, long? reportTemplateGroup)
        {
            var res = r.GetAll();

            if (!string.IsNullOrWhiteSpace(search)) res = res.Where(o => o.Title.ToLower().Contains(search.ToLower()));
            //if (reportTemplateGroup.HasValue) 
                res = res.Where(o => o.GroupId == reportTemplateGroup || reportTemplateGroup == null);
            return View(@"Awesome\LookupList", res);
        }

        public ActionResult Get(Guid id)
        {
            var o = r.Get(id);
            var s = o != null ? string.Format("{0}", o.Title) : "null";
            return Content(s);
        }
    }
}